import wxRequest from "../../utils/wxRequest";
Page({
  data: {
    src: '',
    log:"",
    times:0,
    playTime:0,
    hash:"",
    videoList:"",
    title:"",
    p_id:"",
  },
  onLoad: function (options) {
    this.setData({
      hash:options.hash,
      p_id:options.p_id
    });
    this.initData(options);
  },
  initData:function(options){
    var option={url:"user/play",data:{hash:options.hash,p_id:options.p_id}};
    var that=this;
    wxRequest(option).then(function(result){
      var log=result.data.log;
      var times=0;
      if(log){
        times=log.times
      }
      that.setData({
        log:log,
        times:times,
        src:result.data.video.url,
        videoList:result.data.videoList,
        title:result.data.video.name
      });
    })
  },
  showVideo:function(e){
    var hash=e.currentTarget.dataset.hash;
    this.setData({hash:hash});
    var option={hash:hash,p_id:this.data.p_id};
    this.initData(option);
  },
  onReady(options) {
    this.videoContext = wx.createVideoContext('myVideo');
  },
  onHide() {

  },
  showTime:function(e){
    // console.log(e);
    var times=e.detail.currentTime;
    var playTime=parseInt(times);
    var pTime=this.data.playTime;
    var hash=this.data.hash;
    var uid=wx.getStorageSync('uid');
    if(pTime!=playTime){
      var is_sup=0;
      if(playTime%1800==0){
        is_sup=1;
        this.bindPauseVideo();
      }
      this.setData({playTime:playTime});
      var option={url:"user/recordPut",data:{hash:hash,times:playTime,uid:uid,is_sup:is_sup}};
      wxRequest(option);
    }
  },
  bindPlayVideo() {
    console.log('1')
    this.videoContext.play()
  },
  bindPauseVideo() {
    console.log('1')
    this.videoContext.pause()
  },
  videoErrorCallback(e) {
    console.log('视频错误信息:')
    console.log(e.detail.errMsg)
  }
})